﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Modules.EBOOK.Components
{
    public partial class Book
    {
        #region Private members.

        protected int _ID;
        protected int _CategoryID;
        protected string _ISBN = string.Empty;
        protected string _Title = string.Empty;
        protected string _Authors = string.Empty;
        protected string _Publisher = string.Empty;
        protected DateTime _PublishedDate;
        protected string _CoverImageUrl = string.Empty;
        protected string _Description = string.Empty;
        protected string _FileUrl = string.Empty;
        protected decimal _FileSize;
        protected string _FileType = string.Empty;
        protected string _SourceCodeUrl = string.Empty;
        protected int _TotalComments;
        protected int _TotalViews;
        protected int _TotalDownloads;
        protected string _CreatedBy = string.Empty;
        protected DateTime _CreatedDate;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public int CategoryID
        {
            set { this._CategoryID = value; }
            get { return this._CategoryID; }
        }

        public string ISBN
        {
            set { this._ISBN = value; }
            get { return this._ISBN; }
        }

        public string Title
        {
            set { this._Title = value; }
            get { return this._Title; }
        }

        public string Authors
        {
            set { this._Authors = value; }
            get { return this._Authors; }
        }

        public string Publisher
        {
            set { this._Publisher = value; }
            get { return this._Publisher; }
        }

        public DateTime PublishedDate
        {
            set { this._PublishedDate = value; }
            get { return this._PublishedDate; }
        }

        public string CoverImageUrl
        {
            set { this._CoverImageUrl = value; }
            get { return this._CoverImageUrl; }
        }

        public string Description
        {
            set { this._Description = value; }
            get { return this._Description; }
        }

        public string FileUrl
        {
            set { this._FileUrl = value; }
            get { return this._FileUrl; }
        }

        public decimal FileSize
        {
            set { this._FileSize = value; }
            get { return this._FileSize; }
        }

        public string FileType
        {
            set { this._FileType = value; }
            get { return this._FileType; }
        }

        public string SourceCodeUrl
        {
            set { this._SourceCodeUrl = value; }
            get { return this._SourceCodeUrl; }
        }

        public int TotalComments
        {
            set { this._TotalComments = value; }
            get { return this._TotalComments; }
        }

        public int TotalViews
        {
            set { this._TotalViews = value; }
            get { return this._TotalViews; }
        }

        public int TotalDownloads
        {
            set { this._TotalDownloads = value; }
            get { return this._TotalDownloads; }
        }

        public string CreatedBy
        {
            set { this._CreatedBy = value; }
            get { return this._CreatedBy; }
        }

        public DateTime CreatedDate
        {
            set { this._CreatedDate = value; }
            get { return this._CreatedDate; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Book Load(int iD)
        {
            const string spName = "[dbo].[p_EBOOK_Book_Load]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            Book entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Book();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ISBN"))) entity.ISBN = reader.GetString(reader.GetOrdinal("ISBN"));
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
                if (!reader.IsDBNull(reader.GetOrdinal("Authors"))) entity.Authors = reader.GetString(reader.GetOrdinal("Authors"));
                if (!reader.IsDBNull(reader.GetOrdinal("Publisher"))) entity.Publisher = reader.GetString(reader.GetOrdinal("Publisher"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublishedDate"))) entity.PublishedDate = reader.GetDateTime(reader.GetOrdinal("PublishedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CoverImageUrl"))) entity.CoverImageUrl = reader.GetString(reader.GetOrdinal("CoverImageUrl"));
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) entity.Description = reader.GetString(reader.GetOrdinal("Description"));
                if (!reader.IsDBNull(reader.GetOrdinal("FileUrl"))) entity.FileUrl = reader.GetString(reader.GetOrdinal("FileUrl"));
                if (!reader.IsDBNull(reader.GetOrdinal("FileSize"))) entity.FileSize = reader.GetDecimal(reader.GetOrdinal("FileSize"));
                if (!reader.IsDBNull(reader.GetOrdinal("FileType"))) entity.FileType = reader.GetString(reader.GetOrdinal("FileType"));
                if (!reader.IsDBNull(reader.GetOrdinal("SourceCodeUrl"))) entity.SourceCodeUrl = reader.GetString(reader.GetOrdinal("SourceCodeUrl"));
                if (!reader.IsDBNull(reader.GetOrdinal("TotalComments"))) entity.TotalComments = reader.GetInt32(reader.GetOrdinal("TotalComments"));
                if (!reader.IsDBNull(reader.GetOrdinal("TotalViews"))) entity.TotalViews = reader.GetInt32(reader.GetOrdinal("TotalViews"));
                if (!reader.IsDBNull(reader.GetOrdinal("TotalDownloads"))) entity.TotalDownloads = reader.GetInt32(reader.GetOrdinal("TotalDownloads"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectBy_CategoryID(int categoryID)
        {
            const string spName = "[dbo].[p_EBOOK_Book_SelectBy_CategoryID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_EBOOK_Book_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_EBOOK_Book_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_EBOOK_Book_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_EBOOK_Book_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------


        #region Insert methods.

        public static int Insert(int categoryID, string iSBN, string title, string authors, string publisher, DateTime publishedDate, string coverImageUrl, string description, string fileUrl, decimal fileSize, string fileType, string sourceCodeUrl, int totalComments, int totalViews, int totalDownloads, string createdBy)
        {
            Book entity = new Book();
            entity.CategoryID = categoryID;
            entity.ISBN = iSBN;
            entity.Title = title;
            entity.Authors = authors;
            entity.Publisher = publisher;
            entity.PublishedDate = publishedDate;
            entity.CoverImageUrl = coverImageUrl;
            entity.Description = description;
            entity.FileUrl = fileUrl;
            entity.FileSize = fileSize;
            entity.FileType = fileType;
            entity.SourceCodeUrl = sourceCodeUrl;
            entity.TotalComments = totalComments;
            entity.TotalViews = totalViews;
            entity.TotalDownloads = totalDownloads;
            entity.CreatedBy = createdBy;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_EBOOK_Book_Insert]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, this._CategoryID);
            db.AddInParameter(dbCommand, "@ISBN", SqlDbType.VarChar, this._ISBN);
            db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, this._Title);
            db.AddInParameter(dbCommand, "@Authors", SqlDbType.NVarChar, this._Authors);
            db.AddInParameter(dbCommand, "@Publisher", SqlDbType.NVarChar, this._Publisher);
            db.AddInParameter(dbCommand, "@PublishedDate", SqlDbType.DateTime, this._PublishedDate);
            db.AddInParameter(dbCommand, "@CoverImageUrl", SqlDbType.NVarChar, this._CoverImageUrl);
            db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, this._Description);
            db.AddInParameter(dbCommand, "@FileUrl", SqlDbType.NVarChar, this._FileUrl);
            db.AddInParameter(dbCommand, "@FileSize", SqlDbType.Decimal, this._FileSize);
            db.AddInParameter(dbCommand, "@FileType", SqlDbType.VarChar, this._FileType);
            db.AddInParameter(dbCommand, "@SourceCodeUrl", SqlDbType.NVarChar, this._SourceCodeUrl);
            db.AddInParameter(dbCommand, "@TotalComments", SqlDbType.Int, this._TotalComments);
            db.AddInParameter(dbCommand, "@TotalViews", SqlDbType.Int, this._TotalViews);
            db.AddInParameter(dbCommand, "@TotalDownloads", SqlDbType.Int, this._TotalDownloads);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, this._CreatedBy);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, int categoryID, string iSBN, string title, string authors, string publisher, DateTime publishedDate, string coverImageUrl, string description, string fileUrl, decimal fileSize, string fileType, string sourceCodeUrl, int totalComments, int totalViews, int totalDownloads, string createdBy)
        {
            Book entity = new Book();
            entity.ID = iD;
            entity.CategoryID = categoryID;
            entity.ISBN = iSBN;
            entity.Title = title;
            entity.Authors = authors;
            entity.Publisher = publisher;
            entity.PublishedDate = publishedDate;
            entity.CoverImageUrl = coverImageUrl;
            entity.Description = description;
            entity.FileUrl = fileUrl;
            entity.FileSize = fileSize;
            entity.FileType = fileType;
            entity.SourceCodeUrl = sourceCodeUrl;
            entity.TotalComments = totalComments;
            entity.TotalViews = totalViews;
            entity.TotalDownloads = totalDownloads;
            entity.CreatedBy = createdBy;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_EBOOK_Book_Update]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, this._CategoryID);
            db.AddInParameter(dbCommand, "@ISBN", SqlDbType.VarChar, this._ISBN);
            db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, this._Title);
            db.AddInParameter(dbCommand, "@Authors", SqlDbType.NVarChar, this._Authors);
            db.AddInParameter(dbCommand, "@Publisher", SqlDbType.NVarChar, this._Publisher);
            db.AddInParameter(dbCommand, "@PublishedDate", SqlDbType.DateTime, this._PublishedDate);
            db.AddInParameter(dbCommand, "@CoverImageUrl", SqlDbType.NVarChar, this._CoverImageUrl);
            db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, this._Description);
            db.AddInParameter(dbCommand, "@FileUrl", SqlDbType.NVarChar, this._FileUrl);
            db.AddInParameter(dbCommand, "@FileSize", SqlDbType.Decimal, this._FileSize);
            db.AddInParameter(dbCommand, "@FileType", SqlDbType.VarChar, this._FileType);
            db.AddInParameter(dbCommand, "@SourceCodeUrl", SqlDbType.NVarChar, this._SourceCodeUrl);
            db.AddInParameter(dbCommand, "@TotalComments", SqlDbType.Int, this._TotalComments);
            db.AddInParameter(dbCommand, "@TotalViews", SqlDbType.Int, this._TotalViews);
            db.AddInParameter(dbCommand, "@TotalDownloads", SqlDbType.Int, this._TotalDownloads);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, this._CreatedBy);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            Book entity = new Book();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_EBOOK_Book_Delete]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static int DeleteBy_CategoryID(int categoryID)
        {
            string spName = "[dbo].[p_EBOOK_Book_DeleteBy_CategoryID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);

            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion
    }
}